package com.lakers.notice.springboot.netty.endecode;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lakers.notice.springboot.netty.SendInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.List;

/**
 * 功能描述: MessageProtocolEncoder
 *
 * @author liqin(zxl)
 * @date 2024/5/3
 */
public class MessageProtocolEncoder  extends MessageToMessageEncoder<SendInfo> {
    @Override
    protected void encode(ChannelHandlerContext ctx, SendInfo sendInfo, List<Object> list) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        String text = objectMapper.writeValueAsString(sendInfo);
        TextWebSocketFrame frame = new TextWebSocketFrame(text);
        list.add(frame);
    }
}
